package com.hsc.account;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hsc.db.tools.AccountList;
import com.hsc.db.tools.AccountService;
import com.hsc.db.tools.EmailService;
import com.hsc.db.tools.ItemList;
import com.hsc.db.tools.ItemService;

import android.R.integer;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class CountDialog extends Dialog {
	private static AccountService accs;
	public CountDialog(Context context) {
		super(context);
	}

	public CountDialog(Context context, int theme) {
		super(context, theme);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (accs.dbOpenHelper  != null) {
			accs.dbOpenHelper.close();
		}
	}

	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		private  EditText  txt;

		private String name ="";
		private  HashMap<String, Object> ckItem;

		private SimpleAdapter sim_adapter1;
		private TextView cyear;
		private TextView cyear2;
		private TextView left;
		private TextView right;
		private String curryear="";
		private  TextView btn_count ;
		private GridView gview;
		private List<Map<String, Object>> data_list,data2;
		private String[] Mdata = { "01月", "02月", "03月", "04月",
				"05月", "06月", "07月", "08月",
				"09月", "10月", "11月" ,  "12月"};

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 *
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 *
		 * @param positiveButtonText
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;

			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		public CountDialog create(final Context context,final ItemService items) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final CountDialog dialog = new CountDialog(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.sum, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			accs = new AccountService(context);
			final String [] from ={"text","sum"};
			final int [] to = {R.id.gridtext,R.id.gridsum};

			final Time time = new Time();
			time.setToNow();


			ckItem = new HashMap<String, Object>();
			data_list = new ArrayList<Map<String, Object>>();
			gview = (GridView) layout.findViewById(R.id.gviewmonth);
			cyear = (TextView)layout.findViewById(R.id.cyear2);
			cyear.setText(String.valueOf(time.year)+"年");
//			 cyear.setBackgroundColor(Color.GRAY);
			this.curryear=String.valueOf(time.year);

			left = (TextView)layout.findViewById(R.id.left);
			right = (TextView)layout.findViewById(R.id.right);
//			 cyear2 = (TextView)layout.findViewById(R.id.cyear1);
//			 cyear2.setText(String.valueOf(time.year-1)+"年");
			btn_count =  (TextView)layout.findViewById(R.id.btn_Count);

			double ysum  = accs.getYSum(Integer.parseInt(curryear.substring(0,4)));
			btn_count.setText("总消费("+String.format("%.1f",ysum)+")");


			getData();
			sim_adapter1 = new SimpleAdapter(context, data_list, R.layout.item2, from, to);

			gview.setAdapter(sim_adapter1);

			left.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					int icyear=Integer.parseInt(curryear.substring(0,4));
					curryear=String.valueOf(icyear-1);

					cyear.setText(String.valueOf(icyear-1)+"年");
//				 cyear2.setText(String.valueOf(icyear-2)+"年");

					getData();
					sim_adapter1 = new SimpleAdapter(context, data_list, R.layout.item2, from, to);

					gview.setAdapter(sim_adapter1);

					double ysum  = accs.getYSum(Integer.parseInt(curryear.substring(0,4)));
					btn_count.setText("总消费("+String.format("%.1f",ysum)+")");


				}
			});

			right.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					int icyear=Integer.parseInt(curryear.substring(0,4));
					curryear=String.valueOf(icyear+1);

					cyear.setText(String.valueOf(icyear+1)+"年");
//					 cyear2.setText(String.valueOf(icyear)+"年");

					getData();
					sim_adapter1 = new SimpleAdapter(context, data_list, R.layout.item2, from, to);

					gview.setAdapter(sim_adapter1);

					double ysum  = accs.getYSum(Integer.parseInt(curryear.substring(0,4)));
					btn_count.setText("总消费("+String.format("%.1f",ysum)+")");

				}
			});




			gview.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {

					Object obj = ckItem.get(String.valueOf(position));
					if(obj==null)
					{

						ckItem.put(String.valueOf(position), String.valueOf(position+1));
						view.setBackgroundColor(Color.BLUE);
					}else
					{
						view.setBackgroundColor(Color.WHITE);
						ckItem.remove(String.valueOf(position));
					}
				}

			});

			gview.setOnItemLongClickListener(new OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(AdapterView<?> parent,
											   View view, int position, long id) {

					HashMap<String, Object> item=(HashMap<String, Object>) parent.getItemAtPosition(position);

					String tag =  item.get("text").toString();
					final String month = tag.substring(0,2);

					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage("确定要删除吗？");

					builder.setPositiveButton("删除", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog,
											int which) {
							accs.deleteByMonth(Integer.parseInt(curryear), Integer.parseInt(month));
							getData();
							sim_adapter1 = new SimpleAdapter(context, data_list, R.layout.item2, from, to);

							gview.setAdapter(sim_adapter1);

						}});

					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.dismiss();
						}
					}) ;
					builder.create().show();

					return false;
				}});

			btn_count.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage("确定要上传吗？");

					builder.setPositiveButton("上传", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog,
											int which) {
							if (ckItem.size() > 0) {
								Iterator it = ckItem.keySet()
										.iterator();
								while (it.hasNext()) {
									List<AccountList> list = accs.getScroolDataAdd(
											Integer.parseInt(curryear
													.substring(0, 4)),
											Integer.parseInt(ckItem
													.get(it.next())
													.toString()));
									EmailService.sendMail(list, items,
											accs);
								}
								Toast.makeText(context, "上传成功",
										Toast.LENGTH_SHORT).show();
							} else {
								Toast.makeText(context, "请选择上传月份",
										Toast.LENGTH_SHORT).show();
							}

						}});

					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.dismiss();
						}
					}) ;
					builder.create().show();

				}
			});

			dialog.setContentView(layout);


			return dialog;
		}

		public List<Map<String, Object>> getData(){
			data_list.clear();
//			 Toast.makeText(CountDialog.Builder.this.context,this.curryear.substring(0,4), Toast.LENGTH_SHORT).show();
			for(int i=0;i<Mdata.length;i++){
				Map<String, Object> map = new HashMap<String, Object>();
				double sum =accs.getZSum("1",Integer.parseInt(curryear.substring(0,4)),i+1);
				if(sum==0)
				{
					map.put("sum", "");
				}else
				{
					map.put("sum", String.format("%.1f",sum));
				}

				map.put("text", Mdata[i]);
				data_list.add(map);
			}

			return data_list;
		}

	}


}